﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibGUI.MainForm.UserList;
using LibRTC.Core;
using LibRTC.Core.MessageMap.BuddyEvent;
using LibRTC.Interface;
using RTCCore;

namespace LibGUI.MainForm
{
    public partial class MainForm : Form, IBuddyStatusChange
    {
        private UserTreeView _userTreeView;
        private const int UserTreeViewMargin = 5;

        private void InitializeCustomComponent()
        {
            // initialize my own data structure
            _userTreeView = new UserTreeView();
            _userTreeView.Parent = this;

            _userTreeView.Left = UserTreeViewMargin;
            _userTreeView.Top = UserTreeViewMargin;
            _userTreeView.Width = ClientRectangle.Width - 2 * UserTreeViewMargin;
            _userTreeView.Height = ClientRectangle.Height - 2 * UserTreeViewMargin ;

            _userTreeView.AddUserNode("离线好友", new UserTreeNode("Yuqiang", RTCSystem.GetInstance().GetUserURI("yuqiang")));
            _userTreeView.AddUserNode("离线好友", new UserTreeNode("BaiBai", RTCSystem.GetInstance().GetUserURI("baibai")));
            _userTreeView.AddUserNode("离线好友", new UserTreeNode("YuJP", RTCSystem.GetInstance().GetUserURI("yujp")));

        }

        public MainForm()
        {
            InitializeComponent();
            InitializeCustomComponent();
        }

        private void OnResize(object sender, EventArgs e)
        {
            _userTreeView.Left = UserTreeViewMargin;
            _userTreeView.Top = UserTreeViewMargin;
            _userTreeView.Width = ClientRectangle.Width - 2 * UserTreeViewMargin;
            _userTreeView.Height = ClientRectangle.Height - 2 * UserTreeViewMargin;
            
        }

        private void OnShown(object sender, EventArgs e)
        {
            RTCCoreClient.GetInstance().SubscribeBuddyStatus(RTCSystem.GetInstance().GetUserURI("yuqiang"), "Yuqiang");
            RTCCoreClient.GetInstance().SubscribeBuddyStatus(RTCSystem.GetInstance().GetUserURI("baibai"), "Baibai");
            RTCCoreClient.GetInstance().SubscribeBuddyStatus(RTCSystem.GetInstance().GetUserURI("yujp"), "Yujp");

            RTCCoreClient.GetInstance().AddNotifyWatcher(RTCSystem.GetInstance().GetUserURI("yuqiang"), "Yuqiang");
            RTCCoreClient.GetInstance().AddNotifyWatcher(RTCSystem.GetInstance().GetUserURI("baibai"), "Baibai");
            RTCCoreClient.GetInstance().AddNotifyWatcher(RTCSystem.GetInstance().GetUserURI("yujp"), "Yujp");



            RTCCoreClient.GetInstance().RTCClient.SetLocalPresenceInfo(RTC_PRESENCE_STATUS.RTCXS_PRESENCE_ONLINE,null);
        }

        public void OnBuddyStatusChange(string userURI, RTC_PRESENCE_STATUS status) {
            _userTreeView.OnBuddyStatusChange(userURI,status);
        }

    }
}